package com.example.yls.demoa;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends Service {

    private MyBinder myBinder;

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("king","onBind");
        if(myBinder == null){
            throw new UnsupportedOperationException("Not yet implemented");
        }
        return myBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("king","onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        myBinder = new MyBinder();
        Log.e("king","onCreate");
        new Thread(){
            @Override
            public void run() {
                while (true){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Intent intent = new Intent();
                    intent.setAction("TIME_CHANGED");
                    intent.putExtra("TIME",sdf.format(new Date()));
                    sendBroadcast(intent);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("king","onStartCommand");

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("king","onDestroy");
    }
}
